package simpleHttpClient;

/**
 * Request is a data class storing an HTTP request.  Information fields are accessed using getters and setters.
 * Request implements a toString method returning a properly formatted HTTP request.
 * 
 * @author Rebecca Chandler
 * Scott Wendlandt
 * Li Weihan
 *
 */

public class Request {
	/**
	 * Domain name.  Fills the "Host" field of the HTTP request
	 */
	private String hostName;

	/**
	 * File path.  Follows the "GET" field of the HTTP request
	 */
	private String objectPathName;

	/**
	 * Specifies whether to request a non-persistent connection.
	 */
	private boolean connectionClose;

	/**
	 * System-independent end-of-line string
	 */
	final static String CRLF = "\r\n";

	/**
	 * Empty constructor.
	 */
	public Request(){
		this.connectionClose = true;
	}

	/**
	 * Constructor which accepts a URL in the form of a string
	 */
	public Request(String url){
		this.connectionClose = true;

		url = url.trim().replace("http://", "");
		this.hostName = "http://" + url.substring(0,url.indexOf('/'));
		this.objectPathName = url.substring(url.indexOf('/')+1, url.length());
	}

	/**
	 * Sets the host name
	 * @param hostName
	 */
	public void setHostName(String hostName) {
		hostName = hostName.trim().replace("http://", "");
		this.hostName = hostName;
	}

	/**
	 * Gets the host name
	 * @return host name
	 */
	public String getHostName() {
		return hostName;
	}

	/**
	 * Sets the object path name.  Adds a '/' character to the front of the string if it does not contain one already.
	 * @param objectPathName
	 */
	public void setObjectPathName(String objectPathName) {
		if((objectPathName != null) && ((objectPathName.length() == 0) || (objectPathName.charAt(0) != '/'))){
			this.objectPathName = "/" + objectPathName;
		} else {		
			this.objectPathName = objectPathName;
		}
	}

	/**
	 * Gets the object path name
	 * @return Object path name
	 */
	public String getObjectPathName() {
		return objectPathName;
	}

	/**
	 * Sets whether a persistent connection will be requested in the "Connection:" field of the HTTP request
	 * 
	 * True: non-persistent (close)
	 * False: persistent (keep-alive)
	 * 
	 * @param connectionClose
	 */
	public void setConnectionClose(boolean connectionClose) {
		this.connectionClose = connectionClose;
	}

	/**
	 * Gets the connection status to be requested in the HTTP request in the "Connection:" field
	 * 
	 * True: non-persistent (close)
	 * False: persistent (keep-alive)
	 * 
	 * @return
	 */
	public boolean isConnectionClose() {
		return connectionClose;
	}

	/**
	 * Returns a formatted HTTP request as described in this object.
	 */
	public String toString(){
		String connection = this.connectionClose ? "keep-alive" : "close";

		return("GET " + this.objectPathName + " HTTP/1.1" + CRLF +
				"Host: " + this.hostName + CRLF +
				"Connection: " + connection + CRLF +
				"Accept-language: en-US" + CRLF +
				CRLF );
	}
}
